package com.yc.navy.springmvc.controller;

import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.yc.navy.springmvc.bean.CartInfo;
import com.yc.navy.springmvc.bean.MemberInfo;
import com.yc.navy.springmvc.dto.CartInfoDTO;
import com.yc.navy.springmvc.enums.ResultEnum;
import com.yc.navy.springmvc.mapper.ICartInfoMapper;
import com.yc.navy.springmvc.service.ICartInfoService;
import com.yc.navy.springmvc.util.ConstantIfo;
import com.yc.navy.springmvc.vo.ResultVO;

@RestController
@RequestMapping("/cart")
public class CartInfoController {
	@Autowired
	private ICartInfoMapper cartInfoMapper;
	
	@PostMapping("/add")
	public ResultVO add(CartInfo cf, HttpSession session) {
		Object obj = session.getAttribute(ConstantIfo.CURRENTLOGINMEMBER);
		if(obj == null) {
			return new ResultVO(ResultEnum.LOGIN_ERROR);
		}
		MemberInfo mf = (MemberInfo) obj;
		cf.setMno(mf.getMno());
		String cno = UUID.randomUUID().toString();
		cf.setCno(cno);
		int result = cartInfoMapper.add(cf);
		if(result == -100) {
			return new ResultVO(ResultEnum.CHECK_ERROR);
		}
		if(result > 0) {
			return new ResultVO(ResultEnum.SUCCESS);
		}
		return new ResultVO(ResultEnum.ERROP);	
	}
	
	
	@PostMapping("/update")
	public ResultVO update (CartInfo cf) {
		int result = cartInfoMapper.update(cf);
		if(result == -100) {
			return new ResultVO(ResultEnum.CHECK_ERROR);
		}
		if(result > 0) {
			return new ResultVO(ResultEnum.SUCCESS);
		}
		return new ResultVO(ResultEnum.ERROP);	
	}
	
	@PostMapping("/deleteOne")
	public ResultVO deleteOne (String cno) {
		int result = cartInfoMapper.deleteOne(cno);
		if(result == -100) {
			return new ResultVO(ResultEnum.CHECK_ERROR);
		}
		if(result > 0) {
			return new ResultVO(ResultEnum.SUCCESS);
		}
		return new ResultVO(ResultEnum.ERROP);	
	}
	
	@GetMapping("/list")
	public ResultVO list (HttpSession session) {
		Object obj = session.getAttribute(ConstantIfo.CURRENTLOGINMEMBER);
		if(obj == null) {
			return new ResultVO(ResultEnum.LOGIN_ERROR);
		}
		MemberInfo mf = (MemberInfo) obj;
		System.out.println(mf.getMno());
		List<CartInfo> result = cartInfoMapper.list(mf.getMno());
		if(result != null && !result.isEmpty()) {
			return new ResultVO(ResultEnum.SUCCESS, result);
		}
		return new ResultVO(ResultEnum.DATA_NULL);	
	}
	
	@GetMapping("/findAll")
	public ResultVO findAll (HttpSession session) {
		Object obj = session.getAttribute(ConstantIfo.CURRENTLOGINMEMBER);
		if(obj == null) {
			return new ResultVO(ResultEnum.LOGIN_ERROR);
		}
		MemberInfo mf = (MemberInfo) obj;
		System.out.println(mf.getMno());
		List<CartInfoDTO> result = cartInfoMapper.findAll(mf.getMno());
		if(result != null && !result.isEmpty()) {
			return new ResultVO(ResultEnum.SUCCESS, result);
		}
		return new ResultVO(ResultEnum.DATA_NULL);	
	}
	
	@PostMapping("/finds")
	public ResultVO finds (String cnos) {
		List<CartInfoDTO> list = cartInfoMapper.finds(cnos.split(","));
		System.out.println(list + "123345");
		if(list != null && !list.isEmpty()) {
			return new ResultVO(ResultEnum.SUCCESS, list);
		}
		return new ResultVO(ResultEnum.DATA_NULL);
	}
}
